package com.runfast.common.web.shiro;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.gxuc.runfast.bean.Cuser;
import com.runfast.common.Constants;
import com.runfast.common.utils.TokenUtil;
import com.runfast.paotui.dao.model.Account;
import com.runfast.paotui.dao.model.AccountExample;
import com.runfast.paotui.dao.model.RunfastCuser;
import com.runfast.paotui.service.AccountService;
import com.runfast.paotui.service.RunfastCuserService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

public class UserRealm extends AuthorizingRealm {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserRealm.class);
    @Autowired
    @Lazy
    private AccountService accountService;
    @Autowired
    @Lazy
    private RunfastCuserService runfastCuserService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = (String) principals.getPrimaryPrincipal();

        AccountExample accountExample = new AccountExample();
        accountExample.createCriteria().andUsernameEqualTo(username);
        List<Account> accounts = accountService.selectByExample(accountExample);


        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setStringPermissions(null);

        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String username = upToken.getUsername();
        String password = "";
        if (upToken.getPassword() != null) {
            password = new String(upToken.getPassword());
        }

       /* Account account = null;
        try {
            account = accountService.login(username, password);
        } catch (Exception e) {
            throw new AuthenticationException(e.getMessage(), e);
        }

        RequestContextHolder.currentRequestAttributes().setAttribute(Constants.CURRENT_ACCOUNT, account, RequestAttributes.SCOPE_SESSION);
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(account.getUsername(), password.toCharArray(), getName());*/

        RunfastCuser runfastCuser = null;
        Cuser cuser =  new Cuser();
        try {
            runfastCuser = runfastCuserService.getUserBy(username, password);


//            BeanUtilsBean.getInstance().copyProperties(cuser, runfastCuser);

            PropertyUtils.copyProperties(cuser,runfastCuser);

        } catch (Exception e) {
            throw new AuthenticationException(e.getMessage(), e);
        }

        RequestContextHolder.currentRequestAttributes().setAttribute(Constants.CURRENT_USER, cuser, RequestAttributes.SCOPE_SESSION);
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(runfastCuser.getMobile(), password.toCharArray(), getName());
        return info;
    }
}
